﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using work_log.userservices;
using WebServices;

namespace work_log {
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm {

        //UI线程的同步上下文
        SynchronizationContext m_SyncContext = null;
        public frmLogin() {
            InitializeComponent();
            //获取UI线程同步上下文
            m_SyncContext = SynchronizationContext.Current;
        }

        private void btnLogin_Click(object sender, EventArgs e) {
            if (this.txtName.Text.Trim() == "") {
                this.txtName.Select();
                return;
            } else if (this.txtpwd.Text.Trim() == "") {
                this.txtpwd.Select();
                return;
            }
            this.btnLogin.Enabled = false;
            this.pgWait.Visible = true;
            this.linkLabel1.Enabled = false;
            this.checkBoxX1.Enabled = false;
            //线程
            Thread loginThread = new Thread(new ThreadStart(this.Login));
            loginThread.IsBackground = true;
            loginThread.Start();
        }

        private void Login() {
            try {

                //调用：
                //string url = "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx"; 
                //string[] args = new string[2]; 
                //args[0] = "k123"; 
                //args[1] = ""; 
                //object result = WSHelper.InvokeWebService(url, "getDetailInfoByTrainCode", args); 
                //DataSet ds = (DataSet)result; 
                //this.GridView1.DataSource = ds; 
                //this.GridView1.DataBind(); 

                if (this.txtName.Text != "" && this.txtpwd.Text != "") {

                    //object[] prams = { txtName.Text, txtpwd.Text, Guid.Empty };
                    //object resultTest = WSHelper.InvokeWebService(urlService.userService, Method.Find.ToString(), prams);
                    


                    ResultInfoOfUsers result = new UserServicesSoapClient().Find(txtName.Text, txtpwd.Text, Guid.Empty);
                    if (result.Result) {
                        UserInfo.uid = result.Data.uid;
                        UserInfo.uCode = result.Data.uCode;
                        UserInfo.uName = result.Data.uName;
                        UserInfo.oldPass = UserInfo.uPassword = result.Data.uPassword;
                        UserInfo.uStatus = result.Data.uStatus;
                        UserInfo.createDate = result.Data.CreateDate;
                        UserInfo.updateDate = result.Data.UpdateDate;
                        UserInfo.IsAdmin = false;

                        m_SyncContext.Post(SetSafePost, "登录成功，正在进入主界面 ...");
                    } else {
                        XtraMessageBox.Show(result.MSG);
                    }
                }
            } catch (Exception ex) {
                XtraMessageBox.Show("未能连接到服务器，请稍后再试！");
            }
            m_SyncContext.Post(SetSafePost, "UI");
        }

        private void SetSafePost(object text) {
            if (text.ToString() == "UI") {
                this.pgWait.Visible = false;
                this.btnLogin.Enabled = true;
            } else {
                this.pgWait.Description = text.ToString(); //"登录成功，正在进入主界面 ...";
                if (this.checkBoxX1.Checked) { 
                    
                }
                this.Hide();
                Form frm = new frmMain();
                frm.Show();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e) {
            this.txtName.KeyDown += frmLogin_KeyDown;
            this.txtpwd.KeyDown += frmLogin_KeyDown;
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                btnLogin_Click(sender, e);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            this.Hide();
            frmReg frm = new frmReg();
            DialogResult resultFrm = frm.ShowDialog();
            if (resultFrm == System.Windows.Forms.DialogResult.OK) {
                this.Show();
            }
        }
    }
}